package org.sport.controller;

import org.sport.dbImplement.UserOpeImp;
import org.sport.model.entityModel.UserTest;
import org.sport.model.entityModel.UsertableEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.sport.repository.UserRepository;

import java.util.Date;
import java.util.List;

/**
 * Created by Allen on 2017/9/9.
 */
@Controller
public class MainController {


    // 自动装配数据库接口，不需要再写原始的Connection来操作数据库
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserOpeImp userOperation;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/admin/users", method = RequestMethod.GET)
    public String getUsers(ModelMap modelMap) {


        if(userRepository == null)
        {
            System.out.println("null!!!!");
        }
        // 查询user表中所有记录
        List<UsertableEntity> userList = userRepository.findAll();

        // 将所有记录传递给要返回的jsp页面，放在userList当中
        modelMap.addAttribute("userEntityList", userList);


        if(userList == null)
            System.out.println("111111");
        else
            System.out.println(userList.get(0).toString());


        // 返回pages目录下的admin/users.jsp页面
        return "admin/users";
    }
    // get请求，访问添加用户 页面
    @RequestMapping(value = "/admin/users/add", method = RequestMethod.GET)
    public String addUser() {
        // 转到 admin/addUser.jsp页面
        return "admin/addUser";
    }

    // post请求，处理添加用户请求，并重定向到用户管理页面
    @RequestMapping(value = "/admin/users/addP", method = RequestMethod.POST)
    public String addUserPost(@ModelAttribute("user") UserTest user) {
        // 注意此处，post请求传递过来的是一个UserEntity对象，里面包含了该用户的信息
        // 通过@ModelAttribute()注解可以获取传递过来的'user'，并创建这个对象

        UsertableEntity userReal = new UsertableEntity();

        userReal.setPhoneNumber(user.getPhoneNumber());
        userReal.setPassword(user.getPassword());

        // 数据库中添加一个用户，该步暂时不会刷新缓存
        //userRepository.save(userEntity);

        // 数据库中添加一个用户，并立即刷新缓存
        userRepository.saveAndFlush(userReal);

        // 重定向到用户管理页面，方法为 redirect:url
        return "redirect:/admin/users";
    }



    //调试页面链接
    @RequestMapping(value = "indexfile/proindex/{id}", method = RequestMethod.GET)
    public ModelAndView indexdebug(@PathVariable("id") Integer testId)
    {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id", testId);
        switch(testId)
        {
            case 1:
                modelAndView.setViewName("indexfile/proindex");
                break;
            case 2:
                modelAndView.setViewName("index");
                break;
            case 3:
                modelAndView.setViewName("datapage/libdata1.jsp?"/*+JSONObject.fromObject(shop).toString()*/);
                break;
            //test page1
            case 4:
                modelAndView.addObject("time", new Date());
                modelAndView.setViewName("../../resource/testpage/test.jsp?"/*+JSONObject.fromObject(shop2).toString()*/);
                break;
            //test page2
            case 5:
                modelAndView.setViewName("../../resource/testpage/testByAlish.jsp?"/*+JSONObject.fromObject(shop2).toString()*/);
                break;
            default:break;
        }
        return modelAndView;
    }




    // post请求，处理添加用户请求，并重定向到用户管理页面
    @RequestMapping(value = "/admin/users/update/", method = RequestMethod.GET)
    public String testUpdate() {


        System.out.println("get in update test");
        UsertableEntity user1 = new UsertableEntity();
        user1.setPhoneNumber("123456");
        user1 = userOperation.findUserByPhone(user1);

        user1.setPhoneNumber("334455");
        user1.setPassword("112233");
        userOperation.updateUserEntity(user1);

        return "redirect:/admin/users";
    }

    // post请求，处理添加用户请求，并重定向到用户管理页面
    @RequestMapping(value = "/admin/users/delete/", method = RequestMethod.GET)
    public String testdelUser() {

        System.out.println("get in delete test");
        UsertableEntity user1 = new UsertableEntity();
        user1.setPhoneNumber("334455");
        user1 = userOperation.findUserByPhone(user1);
        userOperation.delUserEntity(user1);

        return "redirect:/admin/users";
    }



//-----------------------------------------测试UserPri------------------------------------------------------------

/*    @RequestMapping(value = "/admin/usersPri",method = RequestMethod.GET)
    public String getUsers(ModelMap modelMap)
    {
        //查询user表中所有记录
        List<UserEntity> userEntityList = userRepository.findAll();

        //将所有记录传递给要返回的jsp页面，放在userEntityList当中
        modelMap.addAttribute("userEntityList",userEntityList);

        //返回pages目录下的admin/users.jsp页面
        return "admin/usersPri";
    }

    // get请求，访问添加用户 页面
    @RequestMapping(value = "/admin/usersPri/add",method = RequestMethod.GET)
    public String addUser()
    {
        //转到 admin/adduser.jsp页面
        return "admin/addUserPri";
    }

    //post请求，处理添加用户请求，并重定向到用户管理页面
    @RequestMapping(value = "/admin/usersPri/addP",method = RequestMethod.POST)
    public String addUserPost(@ModelAttribute("user")UserEntity userEntity)
    {
        //注意此处，post请求传递过来的是一个UserEntity对象，里面包含了该用户的信息
        //通过@ModelAttribute()注解可以获取传递过来的“user”，并创建这个对象

        //数据库中添加一个用户，该步暂时不会刷新缓存
        //userRepository.save(userEntity);

        //数据库中添加一个用户，并立即刷新缓存
        userRepository.saveAndFlush(userEntity);
        return "redirect:/admin/usersPri";
    }

    // 查看用户详情
    // @Pathvariable可以收集URl中的变量，需酦醅的变量用（）括起来
    //  例如：访问 localhost:8080/admin/users/show/1 , 将匹配 id = 1
    @RequestMapping(value = "/admin/usersPri/show/{id}",method = RequestMethod.GET)
    public String showUser(@PathVariable("id")Integer userId, ModelMap modelMap)
    {
        //找到userId所表示的用户
        UserEntity userEntity = userRepository.findOne(userId);

        modelMap.addAttribute("user",userEntity);
        return "admin/userPriDetail";
    }
    // 更新用户信息 操作
    @RequestMapping(value = "/admin/usersPri/updateP",  method = RequestMethod.POST)
    public String updateUserPost(@ModelAttribute("userP") UserEntity userEntity)
    {
        userRepository.updateUser(userEntity.getNickname(),
                userEntity.getFirstName(),
                userEntity.getLastName(),
                userEntity.getPassword(),
                userEntity.getId()
        );
        userRepository.flush(); //刷新缓冲区
        return "redirect:/admin/usersPri";
    }

    //删除用户
    @RequestMapping(value = "/admin/usersPri/delete/{id}", method = RequestMethod.GET)
    public String deletUser(@PathVariable("id") Integer userId)
    {
        userRepository.delete(userId);
        userRepository.flush();
        return "redirect:/admin/usersPri";
    }*/
//---------------------------------------------------------------------------------------------------------
















    public UsertableEntity  findUserByPhone(String phone)
    {
        if(userRepository == null)
        {
            System.out.println("null!!!!");
        }
        return userRepository.finduserByPhoneNumber(phone);
    }

}
